package com.example.front.config;


import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class  JwtUtil {

    private static final String ISS = "manage";
    private static final String SECRET = "secretManage";

    // 过期时间是3600秒，既是1个小时
    private static final long EXPIRATION = 3600L;

    public static String createToken(String username){
        String jwtBuilder =
                Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, SECRET)
                .setIssuer(ISS)
                .setSubject(username)
                .setIssuedAt(new Date())
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRATION*1000))
                .compact();
        return jwtBuilder;
    }

    public static String getUsername(String token){
        return Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJws(token)
                .getBody()
                .getSubject();
    }

    public static boolean isOutTime(String token){
        return Jwts.parser()
                .setSigningKey(SECRET)
                .parseClaimsJws(token)
                .getBody()
                .getExpiration()
                .before(new Date());
    }

}